var ShoppingChartApp = function() {
	var goods=[];
	var invalid_goods=[];
	return {
		init : function() {
			ShoppingChartApp.loadData(1);
			$("body").on('click','label.u4-input[name=checkbox]',function(e){
				e.preventDefault();
				if($(this).hasClass("on-checked")){
					$(this).removeClass('on-checked');
				}else{
					$(this).addClass('on-checked');
				}
				
				var a = $("label.u4-input[name=checkbox].on-checked").length;
				if(a <=0){
					$("#confirmBtn").removeClass('btn-orange').addClass('btn-base2');
				}else{
					$("#confirmBtn").removeClass('btn-base2').addClass('btn-orange');
				}
				
				ShoppingChartApp.calc();
			});
			
			$("body").on('keyup','input[name=num]',function(e){
				e.preventDefault();
				var num = $(this).val();
				var goodsId = $(this).parents('ul.product').find('input[type=checkbox]').val();
				if(Number(num) == NaN || Number(num) < 1){
					layer.open({
						content : '格式不正,只允许输入大于1的整数',
						skin : 'msg',
						time : 2
					// 2秒后自动关闭
					});
					$(this).val(1);
					
					ShoppingChartApp.refresh(goodsId,1);
					return;
				}
				
				
				var num = $(this).val();
				ShoppingChartApp.refresh(goodsId,num);
			});
			
			$(".frm_radio_label").click(function(e){
				e.preventDefault();
				var flag = $(".frm_radio_label").hasClass("selected");
				if(!flag){
					$(".frm_radio_label").addClass("selected");
					$("label.u4-input[name=checkbox]").addClass('on-checked');
				}else{
					$(".frm_radio_label").removeClass("selected");
					$("label.u4-input[name=checkbox]").removeClass('on-checked');
				}
				
				var a = $("label.u4-input[name=checkbox].on-checked").length;
				if(a <=0){
					$("#confirmBtn").removeClass('btn-orange').addClass('btn-base2');
				}else{
					$("#confirmBtn").removeClass('btn-base2').addClass('btn-orange');
				}
				
				ShoppingChartApp.calc();
			});
		},
		calc:function(){
			var target = $("label.u4-input[name=checkbox].on-checked");
			var amount = 0;
			for(var i=0;i<target.length;i++){
				var num = $(target[i]).parents('ul.product').find('input[name=num]').val();
				var price = $(target[i]).parents('ul.product').find('input[name=price]').val();
				
				amount = amount + (num * price);
			}
			$("#totalPrice").text( + amount.toFixed(2));
//			$("#totalNum").text('共' + target.length + '件商品');
		},
		loadData : function(p) {
			var url = $("#APP_ROOT_PATH").val() + '/shopping_chart/page_data';
			var data = {
				page : p
			};

			mui.util.Ajax.asyncGET(url, data, function(res) {
				layer.closeAll();				
				if(res.rows.length > 0){
					goods=[];
					invalid_goods=[];
					for (var i=0;i<res.rows.length;i++)
					{
					if (res.rows[i].invalid) {
						invalid_goods[invalid_goods.length]=res.rows[i];
					}else{
						goods[goods.length]=res.rows[i];
					}
					}
					var data = {
						data : goods
					};
					var bt = baidu.template;
					var html = bt('t:bt', data);
					$("#data-content").html(html);
					if(invalid_goods.length>0){
						$('#isHasValid').show();
						data={
						data:invalid_goods
						};
						html = bt('t:invalid', data);
						$("#data-content-invalid").html(html);
					}else{
						$('#isHasValid').hide();
					}
					$("#noneContent").hide();
				}else{
					$("#noneContent").show();
					$(".main-wrapper").hide();
				}
				
				//更新数量和总价
//				ShoppingChartApp.calcTotal(res.rows);
			}, function() {
				layer.closeAll();
			}, function() {
				layer.open({
					type : 2
				});
			});
		},
		refresh:function(goodsId,num){
			var data = {
				goodsId : goodsId,
				num : num
			};
			var url = $("#APP_ROOT_PATH").val() + 'shopping_chart/create_or_update';
			mui.util.Ajax.asyncGET(url, data, function(res) {
				layer.closeAll();

				if (res.ret != undefined && res.ret != 0) {
					layer.open({
						content : res.message,
						skin : 'msg',
						time : 2
					// 2秒后自动关闭
					});
					return;
				}
				//更新总价
				if(res > 0){
					ShoppingChartApp.calc();
				}
			}, function() {
				layer.closeAll();
			}, function() {
				layer.open({
					type : 2
				});
			});
		},
		add : function(goodsId, e) {
			var num = $(e).parent("div.count_box").find("input[name='num']").val();
			var newNum = parseInt(num) + 1;

			var data = {
				goodsId : goodsId,
				num : newNum
			};
			var url = $("#APP_ROOT_PATH").val() + 'shopping_chart/create_or_update';
			mui.util.Ajax.asyncGET(url, data, function(res) {
				layer.closeAll();

				if (res.ret != undefined && res.ret != 0) {
					layer.open({
						content : res.message,
						skin : 'msg',
						time : 2
					// 2秒后自动关闭
					});
					return;
				}

				//更新总价
				if(res > 0){
					$(e).parent("div.count_box").find("input[name='num']").val(newNum);
					ShoppingChartApp.calc();
				}
			}, function() {
				layer.closeAll();
			}, function() {
				layer.open({
					type : 2
				});
			});
		},
		calcTotal:function(data){
			var price = 0;
			for (var i = 0; i < data.length; i++) {
				price += data[i].amount * data[i].num;
			}
			$("#totalPrice").text("￥" + price.toFixed(2));
//			$("#totalNum").text('共' + data.length + '件商品');
		},
		sub : function(goodsId, e) {
			var num = $(e).parent("div.count_box").find("input[name='num']").val();
			var newNum = parseInt(num) - 1;
			if (newNum == 0) {
				return;
			}

			var data = {
				goodsId : goodsId,
				num : newNum
			};
			var url = $("#APP_ROOT_PATH").val() + 'shopping_chart/create_or_update';
			mui.util.Ajax.asyncGET(url, data, function(res) {
				layer.closeAll();
				if (res.ret != undefined && res.ret != 0) {
					layer.open({
						content : res.message,
						skin : 'msg',
						time : 2
					// 2秒后自动关闭
					});
					return;
				}
				
				//更新总价
				if(res > 0){
					$(e).parent("div.count_box").find("input[name='num']").val(newNum);
					ShoppingChartApp.calc();
				}

			}, function() {
				layer.closeAll();
			}, function() {
				layer.open({
					type : 2
				});
			});
		},
		confirm:function(){
			var selected = $("label.on-checked:not([name=checkboxAll])");
			if(selected.length <= 0){
				layer.open({
					content : '请选择需要购买的产品数量',
					skin : 'msg',
					time : 20
				// 2秒后自动关闭
				});
				return;
			}
			
			var datas = new Array();
			for(var i=0;i<selected.length;i++){
				var goodsId = $(selected[i]).find("input[type=checkbox]").val();
				var num = $(selected[i]).parents('ul.product').find("input[name=num]").val();
				
				var data = {goodsId:goodsId,num:num};
				datas.push(data);
			}
			
			var json = JSON.stringify(datas);
			json = encodeURI(json);
			window.location.href = $("#APP_ROOT_PATH").val() + 'order/confirm?json=' + json;
		},
		delInvalidGoods:function(){
			layer.open({
			    content: '您确定要清除失效商品吗!',
			    btn: ['确定', '取消'],
			    yes: function(index){
			    	var datas = new Array();
					for(var i=0;i<invalid_goods.length;i++){
						var id = invalid_goods[i].id;
						datas.push(id);
					}
					var ids = datas.join(",");
					var data = {ids:ids};
					var url = $("#APP_ROOT_PATH").val() + 'shopping_chart/delete_by_id';
					mui.util.Ajax.asyncGET(url, data, function(res) {
						layer.closeAll();

						if (res.ret != undefined && res.ret != 0) {
							layer.open({
								content : res.message,
								skin : 'msg',
								time : 2
							// 2秒后自动关闭
							});
							return;
						}

						//更新总价
						if(res){
							window.location.reload();
//							ShoppingChartApp.loadData(1);
						}
					}, function() {
						layer.closeAll();
					}, function() {
						layer.open({
							type : 2
						});
					});
			    }
			});
		},
		del:function(){
			var selected = $("label.on-checked:not([name=checkboxAll])");
			if(selected.length <= 0){
				layer.open({
					content : '请选择需要删除的商品',
					skin : 'msg',
					time : 20
				// 2秒后自动关闭
				});
				return;
			}
			
			layer.open({
			    content: '您确定删除商品吗!',
			    btn: ['确定', '取消'],
			    yes: function(index){
			    	var datas = new Array();
					for(var i=0;i<selected.length;i++){
						var id = $(selected[i]).find("input[type=hidden][name=id]").val();
						datas.push(id);
					}
					var ids = datas.join(",");
					var data = {ids:ids};
					
					var url = $("#APP_ROOT_PATH").val() + 'shopping_chart/delete_by_id';
					mui.util.Ajax.asyncGET(url, data, function(res) {
						layer.closeAll();

						if (res.ret != undefined && res.ret != 0) {
							layer.open({
								content : res.message,
								skin : 'msg',
								time : 2
							// 2秒后自动关闭
							});
							return;
						}

						//更新总价
						if(res){
							window.location.reload();
//							ShoppingChartApp.loadData(1);
						}
					}, function() {
						layer.closeAll();
					}, function() {
						layer.open({
							type : 2
						});
					});
			    }
			});
			
		}
	}
}();